$().ready(function(){
	function GetRequest() {   
		   var url = location.search; //获取url中"?"符后的字串   
		   var theRequest = new Object();   
		   if (url.indexOf("?") != -1) {   
		      var str = url.substr(1);   
		      strs = str.split("&");   
		      for(var i = 0; i < strs.length; i ++) {   
		         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);   
		      }   
		   }   
		   return theRequest;   
		}
	var token=GetRequest();
		token=token.login;
	if(token){
		$("#loginAgain").css("display","none");
		return true;
	}else if(token=="false"){
		$("#loginAgain").css("display","block");
	}else{
		$("#loginAgain").css("display","none");
	}
})
   $(function () {
                //登录注册的切换
                $(".head-title-login").on("click","li",function(){
                    $(this).addClass("curr").siblings(".curr").removeClass("curr");
                    var i=$(this).index();
                    if(i==0){
                        $(".login-form-box").css("display","block");
                        $(".register-box").css("display","none");
                    }else{
                        $(".login-form-box").css("display","none");
                        $(".register-box").css("display","block");
                    }
                });
                //忘记密码
                $("#forgetPassword").click(function(){
                    location.href="Token_forgetpwd.html";
                })
                $("#loginBack").click(function(){
                    location.href="Token_My.html"
                });
                $(".form-login input").click(function(){
                	$("#loginAgain").css("display","none");
                })
                $("input").focus(function () {
                    $("#noticeError").css("display","none");
                    $("#tipError").css("display","none");
                });
                //登录提交Ajax请求
                $("#loginBtn").on("click",function(){
                    var tEmail=$("#tokenEmail").val();
                    var tPassWord=$("#tokenPsd").val();
                    var regEmail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                    var regPwd= /^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z]).*$/;
                    if(tEmail.length==0){
                        $("#noticeError").html("邮箱地址不能为空").css("display","block");
                        return true;
                    }else if(!regEmail.test(tEmail)){
                        $("#noticeError").html("请输入正确的邮箱地址").css("display","block");
                        return true;
                    }
                    if(tPassWord.length==0){
                        $("#noticeError").html("密码不能为空").css("display","block");
                        return true;
                    } else if(!regPwd.test(tPassWord)){
                        $("#noticeError").html("密码长度至少6位，包含数字和字母").css("display","block");
                        return true;
                    }
                    var more={tEmail:tEmail,tPassWord:tPassWord};
                    $.ajax({
                        url:"/app/user_registered/login",
                        data:more,
                        success:function(r){
                            //如果登录成功 跳转页面
                            console.log(r)
                            if(r.code==0){
                               sessionStorage.setItem("token",r.token);
                               //$("#noticeError").html("登录成功").css("display","block");
                               window.location.href = "../Token_Start.html";
                            }else{
                                $("#noticeError").html(r.msg).css("display","block");
                            }
                        }
                    })
                });
                //获取验证码
                $("#verifyCode").click(function(){
                    var regIptEmail=$("#regstEmail").val();
                    $(this).html("获取中");
                    var regEmail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                    if(regIptEmail.length==0){
                        $("#tipError").html("邮箱地址不能为空").css("display","block");
                        return true;
                    }else if(!regEmail.test(regIptEmail)){
                        $("#tipError").html("请输入正确的邮箱地址").css("display","block");
                        return true;
                    }
                    //发送AJAX
                     $.ajax({
                    	type:'post',  
                    	url:"/app/user_registered/registerCandy",
                        data:{"regIptEmail":regIptEmail},
                        success:function(r){
                           // console.log(r);
                            if(r.code==0){
                            	$("#verifyCode").html("获取成功");
                            }else{
                            	$("#verifyCode").html("重新获取");
                            	alert(r.msg)
                            }
                        }
                    })
                });
                //注册提交 ajax
                $("#registerBtn").click(function () {
                    var regEmail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                    var regPwd= /^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z]).*$/;
                    var regInputEmail=$("#regstEmail").val();
                    var regInputPwd=$("#regstPwd").val();
                    var regInputRepwd=$("#againPwd").val();
                    var regInvited=$("#invitedCode").val();
                    var code=$("#code").val();
                    //判断邮箱
                    if(regInputEmail.length==0){
                        $("#tipError").html("邮箱地址不能为空").css("display","block");
                        return true;
                    }else if(!regEmail.test(regInputEmail)){
                        $("#tipError").html("请输入正确的邮箱地址").css("display","block");
                        return true;
                    }
                    //判断输入密码
                    if(regInputPwd.length==0){
                        $("#tipError").html("密码不能为空").css("display","block");
                        return true;
                    }else if(!regPwd.test(regInputPwd)){
                        $("#tipError").html("密码长度至少6位，包含数字和字母").css("display","block");
                        return true;
                    }
                    //判断确认密码
                    if(regInputRepwd.length==0){
                        $("#tipError").html("确认密码不能为空").css("display","block");
                        return true;
                    }else if(regInputRepwd!==regInputPwd){
                        $("#tipError").html("两次密码不一致").css("display","block");
                        return true;
                    }
                    var more={email:regInputEmail,pwd:regInputPwd,verifyCode:regInvited,code:code};
                    //console.log("more register",more);
                    $.ajax({
                        url:"/app/user_registered/registered",
                        data:more,
                        success:function(r){
                           // console.log(r);
                            if(r.code==0){
								var id = r.id;
								$.ajax({
			                        url:"/app/user_registered/registeredtwo",
			                        data:{"id":id},
			                        success:function(r){
			                           // console.log(r);
			                            if(r.code==0){
			                            	alert("注册成功！")
			                            	location.reload();
			                            	
			                            	
			                            	/*$("#tipError").html("注册成功").css("display","block");
			                            	$("#tipError").click(function(){
			                            		location.reload();
			                            	})*/
			                            }else{
			                            	$("#tipError").html(r.msg).css("display","block");
			                            }
			                        }
			                    })
                            }
                        }
                    })
                });

            });